package com.imooc.controller;

import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.pojo.vo.NewItemsVO;
import com.imooc.service.CategoryService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@Api(value = "商品管理", tags = {"用于商品管理的相关接口"})
@RestController
@RequestMapping({"/category"})
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "查询一级商品分类", notes = "查询一级商品分类", httpMethod = "GET")
    @GetMapping("/queryRootCategory")
    public IMOOCJSONResult queryRootCategory() {
        // 获取redis中关于一级分类缓存数据
        List<Category> categories;
        String rootCategory = redisOperator.get("rootCategory");
        if (StringUtils.isBlank(rootCategory)) {
            categories = categoryService.queryRootCategory();
            redisOperator.set("rootCategory", JsonUtils.objectToJson(categories));
        } else {
            categories = JsonUtils.jsonToList(rootCategory, Category.class);
        }
        return IMOOCJSONResult.ok(categories);
    }

    @ApiOperation(value = "根据一级分类获取子分类", notes = "根据一级分类获取子分类", httpMethod = "GET")
    @GetMapping("/getSubCategoryList/{root}")
    public IMOOCJSONResult getSubCategoryList(@ApiParam(name = "root", value = "一级分类id", required = true)
                                              @PathVariable("root") Integer root) {

        if (Objects.isNull(root)) {
            return IMOOCJSONResult.errorMsg("一级分类id不能为空");
        }
        List<CategoryVO> categories;
        String rootCategory = redisOperator.get("firstCategory");
        if (StringUtils.isBlank(rootCategory)) {
            categories = categoryService.getSubCategoryList(root);
            redisOperator.set("firstCategory", JsonUtils.objectToJson(categories));
        } else {
            categories = JsonUtils.jsonToList(rootCategory, CategoryVO.class);
        }
        return IMOOCJSONResult.ok(categories);
    }

    @ApiOperation(value = "根据一级分类最新商品六条数据", notes = "根据一级分类最新商品六条数据", httpMethod = "GET")
    @GetMapping("/getNewSixItems/{root}")
    public IMOOCJSONResult getNewSixItems(@ApiParam(name = "root", value = "一级分类id", required = true)
                                          @PathVariable("root") Integer root) {
        if (Objects.isNull(root)) {
            return IMOOCJSONResult.errorMsg("一级分类id不能为空");
        }
        List<NewItemsVO> newSixItems;
        String rootCategory = redisOperator.get("newSixItems");
        if (StringUtils.isBlank(rootCategory)) {
            newSixItems = categoryService.getNewSixItems(root);
            redisOperator.set("firstCategory", JsonUtils.objectToJson(newSixItems));
        } else {
            newSixItems = JsonUtils.jsonToList(rootCategory, NewItemsVO.class);
        }
        return IMOOCJSONResult.ok(newSixItems);
    }

}